// pages/shopDetail.js
import {
  queryShopById,
  queryShopOrderList,
  createOrder
} from '../../api/shop'

import Toast from '@vant/weapp/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopOrderList: [],
    shopInfo: {},
    orderRemark: null,
    startTime: "",
    endTime: ""
  },


  queryShop(id) {
    let that = this;
    queryShopById(id)
      .then(res => {
        that.setData({
          shopInfo: res.data.data
        })
      })
      .catch(err => {
        console.log('error', error);
        Toast.error('请求失败')
      })
  },

  queryOrderList(id) {
    let that = this;
    queryShopOrderList(id)
      .then(res => {
        that.setData({
          shopOrderList: res.data.data
        })
      }).catch(err => {
        console.log('error', error);
        Toast.error('请求失败')
      })
  },


  confirmOrder() {
  
    if (!this.data.startTime && !this.data.endTime) {
      wx.showToast({
        title: '请选择时长',
      })
      return;
    }

    let orderRemark = this.data.orderRemark;
    if (!orderRemark) {
      wx.showToast({
        title: '请输入订单备注',
      })
      return;
    }
    createOrder(this.data.startTime,this.data.endTime, orderRemark, this.data.shopInfo.id)
      .then(res => {
        console.log(res, "Order Success")
        if (res.data.code === 200) {
          // navigate to order
          wx.showToast({
            'title': '成功'
          });
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 1000);
        }
      })

  },

  bindKeyInput(e) {
    this.setData({
      orderRemark: e.detail.value
    })
  },

  changeStartTime(e){

    this.setData({
      startTime: e.detail.value
    })
  },

  changeEndTime(e){
    this.setData({
      endTime: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // TODO ID
    let shopId = options.id;
    this.queryShop(shopId);
    this.queryOrderList(shopId);
  },

  goBack() {
    wx.switchTab({
      url: '/pages/index/index'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})